<template>
  <el-row style='margin: 10px'>
    <el-col :span="8" style="padding-right: 10px">
      <div class="grid-content bg-purple">
        <el-card>
          <div class="user">
            <img v-if="url" :src="url"/>
            <img v-else src="@/assets/images/profile1.jpg" alt="" />
            <div class="userInfo">
              <p class="name">{{name}}</p>
              <p class="access">{{realName}}</p>
            </div>
          </div>
          <div class="userLogin">
            <p class="time">登录时间：<span>{{timeNow}}</span></p>
            <p class="address">登录地点：<span>{{address}}</span></p>
          </div>
        </el-card>
        <!-- 图标部分 -->
        <el-card style="margin-top: 20px; height: 360px">
            <h3>个人排行榜</h3>
            <el-table
              :data="tableData"
              stripe
              style="width: 100%">
              <el-table-column
                prop="userName"
                label="姓名">
              </el-table-column>
              <el-table-column
                prop="userScore"
                label="成绩">
              </el-table-column>
              <el-table-column
                label="排名">
                <template slot-scope="scope">
                  <img :src="require(`@/assets/images/${scope.$index+1}.png`)" alt="" width="16px" height="16px">
                </template>
              </el-table-column>
              <el-table-column
                label="用户时长">
                <template slot-scope="scope">
                  <span>{{scope.row.userTime}}min</span>
                </template>
              </el-table-column>
            </el-table>
        </el-card>
      </div>
    </el-col>
    <el-col :span="16" style="padding-left: 10px">
      <div class="grid-content bg-purple-light">
        <div class="num">
          <el-card
            v-for="item in countData"
            :key="item.name"
            :body-style="{ display: 'flex', padding: 0 }"
          >
            <i
              :class="`el-icon-${item.icon}`"
              :style="{ background: item.color }"
            ></i>
            <div class="content">
              <p class="price">{{ item.value }}次</p>
              <p class="desc">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
      </div>
      <el-card style="height: 250px">
        <!-- 折线图 -->
        <LineChart/>
      </el-card>
      <div class="graph">
        <el-card style="height: 240px">
          <!-- 柱状图 -->
          <Bar/>
        </el-card>
        <el-card style="height: 240px">
          <!-- 柱状图 -->
          <PieChart/>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { mapGetters } from "vuex";
import config from "../../../package.json";
import {getUser} from "@/api/user"
//引入echars表：
import Bar from "../echars/Bar"
import LineChart from "../echars/LineChart"
import PieChart from "../echars/PieChart"
import {getData} from "@/api/sys/user/user"
//获取位置信息
import myBMap from "@/utils/myBMap";


export default {
  name: "Dashboard",
  data() {
    return {
      timeNow: '',
      address:'',   //获取位置
      imageUrl:'',
      flag:'',
      currentRole: "adminDashboard",
      version: config.version,
      tableData: [],
      countData: [
        {
          name: "今日考试次数",
          value: 10,
          icon: "sunrise",
          color: "#2ec7c9",
        },
        {
          name: "本月考试次数",
          value: 20,
          icon: "sunny",
          color: "#ffb980",
        },
        {
          name: "本学年考试次数",
          value: 40,
          icon: "moon",
          color: "#5ab1ef",
        },
      ]
    };
  },
  computed: {
    ...mapGetters(["roles", "siteData","realName",'name','userId','info','url','time']),
  },
  created(){
	  this.getLocation();
  },
  mounted(){
    getUser(this.userId).then((response)=>{
      //设置用户头像：
      this.flag = response.data.image
      //获取用户相关信息
      this.$store.dispatch('user/getUser',this.userId);
    }),
    getData().then(({data})=>{
      this.tableData = data.records
    }),
    this.getNowDate()
  },
  methods:{
    /**获取地图定位*/
    getLocation() {
      let _that = this;
      let geolocation = location.initMap("map-container"); //定位
      AMap.event.addListener(geolocation, "complete", result => {
        _that.lat = result.position.lat;
        _that.lng = result.position.lng;
        _that.province = result.addressComponent.province;
        _that.city = result.addressComponent.city;
        _that.district = result.addressComponent.district;
      });
    },
    getNowDate() {
      var myDate = new Date;
      var year = myDate.getFullYear(); //获取当前年
      var mon = myDate.getMonth() + 1; //获取当前月
      var date = myDate.getDate(); //获取当前日
      var hours = myDate.getHours(); //获取当前小时
      var minutes = myDate.getMinutes(); //获取当前分钟
      var seconds = myDate.getSeconds(); //获取当前秒
      var now = year + "-" + mon + "-" + date + " " + hours + ":" + minutes + ":" + seconds;
      this.timeNow = now
    },
    //获取当前位置：
    getLocation() {
      //Toast("如长时间未获取办理区域请手动选择");
      myBMap.init().then(() => {
        let that = this;
        let geolocation = new BMap.Geolocation(); 
        // 创建百度地理位置实例，代替 navigator.geolocation
        geolocation.getCurrentPosition(function (e) {
          if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            // 百度 geolocation 的经纬度属性不同，此处是 point.lat 而不是 coords.latitude
            let point = new BMap.Point(e.point.lng, e.point.lat);
            let gc = new BMap.Geocoder();
            gc.getLocation(point, function (rs) {
              //console.log(rs);
              that.address = rs.address
              //<<<<<<<<<<<<<<<<需要的位置信息在这获取
            });
          } else {
            Toast("定位失败，请手动选择区域或重新定位");
            this.showloading = false;
          }
        });
      });
    }
  },
  components:{
    Bar,
    LineChart,
    PieChart
  }
};
</script>

<style lang='less' scoped>
.user {
  display: flex;
  //纵轴垂直居中：
  align-items: center;
  padding-bottom: 20px;
  margin-bottom: 20px;
  border-bottom: 1px solid #ccc;
  img {
    margin-right: 40px;
    width: 130px;
    height: 130px;
    border-radius: 50%;
  }
  .userInfo {
    .name {
      font-size: 30px;
      font-style: 500px;
      margin-bottom: 10px;
    }
    .access {
      font-size: 15px;
      color: gray;
    }
  }
}
.userLogin {
  p {
    line-height: 28px;
    font-size: 14px;
    color: #999999;
    span {
      margin-left: 60px;
      color: #666666;
    }
  }
}
.num {
  display: flex;
  flex-wrap: wrap;
  //  使元素进行贴边
  justify-content: space-between;
  .el-card {
    width: 32%;
    margin-bottom: 20px;
    i {
      width: 80px;
      height: 80px;
      line-height: 80px;
      text-align: center;
      font-size: 30px;
      color: #fff;
    }
    .content {
      //纵轴垂直居中：
      display: flex;
      flex-direction: column;
      justify-content: center;
      margin-left: 15px;
      .price {
        font-size: 30px;
        margin-bottom: 10px;
        height: 30px;
        line-height: 30px;
      }
      .desc {
        font-size: 14px;
        color: #999;
        text-align: center;
      }
    }
  }
}
.graph {
  display: flex;
  //  使元素进行贴边
  justify-content: space-between;
  margin-top: 20px;
  .el-card {
    width: 48%;
    margin: 0;
    padding: 0;
  }
}
p{
  margin: 0;
  padding: 0;
}
table{
  color: #909399;
  font-size: 14px;
  border-collapse:collapse;
  tr{
    border-bottom: 1px solid #EBEEF5;
    &:nth-child(2n){
      &:hover{
        background: oldlace;
      }
    }
    &:nth-child(2n+3){
      &:hover{
        background: #f0f9eb;
      }
    }
    height: 50px;
    td{
      width: 25%;
      text-align: center;
    }
  }
}
.graph{
    display: flex;
    //  使元素进行贴边
    justify-content: space-between;
    margin-top: 20px;
    .el-card{
        width: 48%;
        margin: 0;
        padding: 0;
    }
}
</style>
